/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * QueryAFAPlayersViewImplDialog.java
 *
 * Created on 19-feb-2010, 17:52:47
 */

package tpdisenio2009.mvp.display;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import tpdisenio2009.model.beans.RealTeam;
import tpdisenio2009.model.beans.SoccerPlayer;

/**
 *
 * @author Pablito
 */
public class QueryAFAPlayersViewImpl extends javax.swing.JDialog implements QueryAFAPlayersView {
    
    private String windowPosition;
    private int budget;
    private List<SoccerPlayer> players;

    /** Creates new form QueryAFAPlayersViewImplDialog
     * Importante!!! Si el parámetro position es null, significa que la posición puede ser seteada
     * en la ventana. Caso contrario se debe deshabilitar el JComboBox Position.
     **/

    public QueryAFAPlayersViewImpl(java.awt.Frame parent, boolean modal, String position, int avaliableBudget, List<SoccerPlayer> soccerPlayers ) {
        super(parent, modal);
        setWindowPosition( position );
        budget = avaliableBudget;
        players = soccerPlayers;
        initComponents();

        Dimension dim = this.getToolkit().getScreenSize();
        this.setLocation((dim.width - this.getWidth()) / 2,(dim.height - this.getHeight()) / 2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelListaInterna = new javax.swing.JPanel();
        jScrollPaneLista = new javax.swing.JScrollPane();
        jTableLista = new javax.swing.JTable();
        jButtonAgregarJugador = new javax.swing.JButton();
        jComboBoxPosicion = new javax.swing.JComboBox();
        jButtonBuscar = new javax.swing.JButton();
        jLabelTeQuedan = new javax.swing.JLabel();
        jTextFieldCotizacionMax = new javax.swing.JTextField();
        jButtonSalir = new javax.swing.JButton();
        jLabelPosicion = new javax.swing.JLabel();
        jLabelGastado = new javax.swing.JLabel();
        jLabelCotizacion = new javax.swing.JLabel();
        jLabelRestante = new javax.swing.JLabel();
        jTextFieldCotizacionMin = new javax.swing.JTextField();
        jLabelClub = new javax.swing.JLabel();
        jComboBoxClub = new javax.swing.JComboBox();
        jLabelLlevasGastado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tpdisenio2009.Tpdisenio2009App.class).getContext().getResourceMap(QueryAFAPlayersViewImpl.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jPanelListaInterna.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanelListaInterna.border.title"))); // NOI18N
        jPanelListaInterna.setName("jPanelListaInterna"); // NOI18N

        jScrollPaneLista.setName("jScrollPaneLista"); // NOI18N

        jTableLista.setAutoCreateRowSorter(true);
        jTableLista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                " Jugador", "Club", "Posición", "Cotización"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableLista.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTableLista.setName("jTableLista"); // NOI18N
        jTableLista.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableLista.getTableHeader().setReorderingAllowed(false);
        jScrollPaneLista.setViewportView(jTableLista);
        jTableLista.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("jTableLista.columnModel.title0")); // NOI18N
        jTableLista.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("jTableLista.columnModel.title1")); // NOI18N
        jTableLista.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("jTableLista.columnModel.title2")); // NOI18N
        jTableLista.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("jTableLista.columnModel.title3")); // NOI18N

        jButtonAgregarJugador.setText(resourceMap.getString("jButtonAgregarJugador.text")); // NOI18N
        jButtonAgregarJugador.setToolTipText(resourceMap.getString("jButtonAgregarJugador.toolTipText")); // NOI18N
        jButtonAgregarJugador.setName("jButtonAgregarJugador"); // NOI18N

        javax.swing.GroupLayout jPanelListaInternaLayout = new javax.swing.GroupLayout(jPanelListaInterna);
        jPanelListaInterna.setLayout(jPanelListaInternaLayout);
        jPanelListaInternaLayout.setHorizontalGroup(
            jPanelListaInternaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelListaInternaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelListaInternaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPaneLista, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                    .addComponent(jButtonAgregarJugador))
                .addContainerGap())
        );
        jPanelListaInternaLayout.setVerticalGroup(
            jPanelListaInternaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelListaInternaLayout.createSequentialGroup()
                .addComponent(jScrollPaneLista, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonAgregarJugador)
                .addContainerGap())
        );

        jComboBoxPosicion.setToolTipText(resourceMap.getString("jComboBoxPosicion.toolTipText")); // NOI18N
        jComboBoxPosicion.setName("jComboBoxPosicion"); // NOI18N

        jButtonBuscar.setText(resourceMap.getString("jButtonBuscar.text")); // NOI18N
        jButtonBuscar.setToolTipText(resourceMap.getString("jButtonBuscar.toolTipText")); // NOI18N
        jButtonBuscar.setName("jButtonBuscar"); // NOI18N

        jLabelTeQuedan.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelTeQuedan.setText(resourceMap.getString("jLabelTeQuedan.text")); // NOI18N
        jLabelTeQuedan.setName("jLabelTeQuedan"); // NOI18N

        jTextFieldCotizacionMax.setToolTipText(resourceMap.getString("jTextFieldCotizacionMax.toolTipText")); // NOI18N
        jTextFieldCotizacionMax.setName("jTextFieldCotizacionMax"); // NOI18N

        jButtonSalir.setText(resourceMap.getString("jButtonSalir.text")); // NOI18N
        jButtonSalir.setToolTipText(resourceMap.getString("jButtonSalir.toolTipText")); // NOI18N
        jButtonSalir.setName("jButtonSalir"); // NOI18N

        jLabelPosicion.setText(resourceMap.getString("jLabelPosicion.text")); // NOI18N
        jLabelPosicion.setName("jLabelPosicion"); // NOI18N

        jLabelGastado.setFont(resourceMap.getFont("jLabelGastado.font")); // NOI18N
        jLabelGastado.setText(resourceMap.getString("jLabelGastado.text")); // NOI18N
        jLabelGastado.setName("jLabelGastado"); // NOI18N

        jLabelCotizacion.setText(resourceMap.getString("jLabelCotizacion.text")); // NOI18N
        jLabelCotizacion.setName("jLabelCotizacion"); // NOI18N

        jLabelRestante.setFont(resourceMap.getFont("jLabelRestante.font")); // NOI18N
        jLabelRestante.setText(resourceMap.getString("jLabelRestante.text")); // NOI18N
        jLabelRestante.setName("jLabelRestante"); // NOI18N

        jTextFieldCotizacionMin.setToolTipText(resourceMap.getString("jTextFieldCotizacionMin.toolTipText")); // NOI18N
        jTextFieldCotizacionMin.setName("jTextFieldCotizacionMin"); // NOI18N

        jLabelClub.setText(resourceMap.getString("jLabelClub.text")); // NOI18N
        jLabelClub.setName("jLabelClub"); // NOI18N

        jComboBoxClub.setToolTipText(resourceMap.getString("jComboBoxClub.toolTipText")); // NOI18N
        jComboBoxClub.setName("jComboBoxClub"); // NOI18N

        jLabelLlevasGastado.setText(resourceMap.getString("jLabelLlevasGastado.text")); // NOI18N
        jLabelLlevasGastado.setName("jLabelLlevasGastado"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanelListaInterna, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabelClub)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxClub, 0, 90, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabelCotizacion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldCotizacionMin, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldCotizacionMax, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabelPosicion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxPosicion, 0, 83, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonBuscar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabelLlevasGastado)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelGastado)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelTeQuedan, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelRestante)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 183, Short.MAX_VALUE)
                        .addComponent(jButtonSalir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelClub)
                    .addComponent(jButtonBuscar)
                    .addComponent(jComboBoxClub, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxPosicion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelPosicion)
                    .addComponent(jLabelCotizacion)
                    .addComponent(jTextFieldCotizacionMin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldCotizacionMax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelListaInterna, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelLlevasGastado)
                    .addComponent(jLabelGastado)
                    .addComponent(jLabelRestante)
                    .addComponent(jLabelTeQuedan)
                    .addComponent(jButtonSalir))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAgregarJugador;
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonSalir;
    private javax.swing.JComboBox jComboBoxClub;
    private javax.swing.JComboBox jComboBoxPosicion;
    private javax.swing.JLabel jLabelClub;
    private javax.swing.JLabel jLabelCotizacion;
    private javax.swing.JLabel jLabelGastado;
    private javax.swing.JLabel jLabelLlevasGastado;
    private javax.swing.JLabel jLabelPosicion;
    private javax.swing.JLabel jLabelRestante;
    private javax.swing.JLabel jLabelTeQuedan;
    private javax.swing.JPanel jPanelListaInterna;
    private javax.swing.JScrollPane jScrollPaneLista;
    private javax.swing.JTable jTableLista;
    private javax.swing.JTextField jTextFieldCotizacionMax;
    private javax.swing.JTextField jTextFieldCotizacionMin;
    // End of variables declaration//GEN-END:variables

    public void setClubs(List<RealTeam> clubs) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();

        RealTeam allClubs = new RealTeam();
        allClubs.setName("Todos");
        model.addElement( allClubs );

        for (RealTeam club : clubs)
               model.addElement( club );
        
        jComboBoxClub.setModel( model );
    }

    public RealTeam getClub() {
        DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxClub.getModel();
        RealTeam selectedClub = (RealTeam) model.getSelectedItem();
        return selectedClub;
    }

    public void addClubChangeListener(ItemListener listener) {
        jComboBoxClub.addItemListener( listener );
    }

    public void removeClubChangeListener(ItemListener listener) {
        jComboBoxClub.removeItemListener( listener );
    }

    public void setMinCotization(Integer minCotization) {
        jTextFieldCotizacionMin.setText( minCotization.toString() );
    }

    public Integer getMinCotization() {
        Integer minCotization = new Integer( jTextFieldCotizacionMin.getText() );
        return minCotization;
    }

    public boolean isMinCotizationEmpty() {
         return jTextFieldCotizacionMin.getText().isEmpty();
    }

    public String getMinCotizationString() {
        return jTextFieldCotizacionMin.getText();
    }

    public void addMinCotizationChangeListener(ActionListener listener) {
        jTextFieldCotizacionMin.addActionListener( listener );
    }

    public void removeMinCotizationChangeListener(ActionListener listener) {
        jTextFieldCotizacionMin.removeActionListener( listener );
    }

    public void setMaxCotization(Integer maxCotization) {
        jTextFieldCotizacionMax.setText( maxCotization.toString() );
    }

    public Integer getMaxCotization() {
        Integer maxCotization = new Integer( jTextFieldCotizacionMax.getText() );
        return maxCotization;
    }

    public boolean isMaxCotizationEmpty() {
         return jTextFieldCotizacionMax.getText().isEmpty();
    }

    public String getMaxCotizationString() {
        return jTextFieldCotizacionMax.getText();
    }

    public void addMaxCotizationChangeListener(ActionListener listener) {
        jTextFieldCotizacionMax.addActionListener( listener );
    }

    public void removeMaxCotizationChangeListener(ActionListener listener) {
        jTextFieldCotizacionMax.removeActionListener( listener );
    }

    public int getBudget() {
        return budget;
    }

    public List<SoccerPlayer> getSoccerPlayers() {
        return players;
    }

    public void setPosition(List<String> positions) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (String position : positions)
        model.addElement( position );
        jComboBoxPosicion.setModel( model );
    }

    public String getCurrentPosition() {
        DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxPosicion.getModel();
        String selectedPosition = (String) model.getSelectedItem();
        return selectedPosition;
    }

    public void setSelectedPosition( String position ) {
        jComboBoxPosicion.setSelectedItem( position );
    }

    public void disablePosition() {        
        jComboBoxPosicion.setEnabled(false);
    }

    public void addPositionChangeListener(ItemListener listener) {
        jComboBoxPosicion.addItemListener( listener );
    }

    public void removePositionChangeListener(ItemListener listener) {
        jComboBoxPosicion.removeItemListener( listener );
    }

    public void loadSoccerPlayers(List<SoccerPlayer> soccerPlayers) {
         QueryAFAPlayerTableModel model = new QueryAFAPlayerTableModel( soccerPlayers );
         jTableLista.setModel( model );
    }

    public SoccerPlayer getSelectedPlayer() {

        if (jTableLista.getSelectedRow() >= 0) {
            QueryAFAPlayerTableModel model = (QueryAFAPlayerTableModel) jTableLista.getModel();

            return model.getSoccerPlayer( jTableLista.convertRowIndexToModel(jTableLista.getSelectedRow()) );
        } else {
            return null;
        }
    }

    public void setLlevasGastado(Integer gastado) {
        jLabelGastado.setText( String.valueOf( gastado ) );
    }

    public void setTeQuedan(Integer quedan) {
        jLabelRestante.setText( String.valueOf( quedan ) );
    }

    public void addSearchPlayerListener(ActionListener listener) {
        jButtonBuscar.addActionListener( listener );
    }

    public void removeSearchPlayerListener(ActionListener listener) {
        jButtonBuscar.removeActionListener( listener );
    }

    public void addAddPlayerListener(ActionListener listener) {
        jButtonAgregarJugador.addActionListener( listener );
    }

    public void removeAddPlayerListener(ActionListener listener) {
        jButtonAgregarJugador.removeActionListener( listener );
    }

    public void addExitListener(ActionListener listener) {
        jButtonSalir.addActionListener( listener );
    }

    public void removeExitListener(ActionListener listener) {
        jButtonSalir.removeActionListener( listener );
    }

    // Se refiere a la posicion que se pasó por parámetro.
    public String getWindowPosition() {
        return windowPosition;
    }

    public void close() {
        setDefaultCloseOperation( DISPOSE_ON_CLOSE );
        setVisible( false );
    }

    public void showError( int focusElement, String errorMessage, String titleMessageWindow ) {
        JOptionPane.showMessageDialog( this,
                errorMessage,
                titleMessageWindow,
                JOptionPane.ERROR_MESSAGE);
        switch (focusElement) {
            case MIN_ERROR:   jTextFieldCotizacionMin.requestFocus(); break;
            case MAX_ERROR:   jTextFieldCotizacionMax.requestFocus(); break;
        }
    }

    public void showNotification( String notifiactionMessage, String notificationMessageTitle ) {
        JOptionPane.showMessageDialog( this,
                notifiactionMessage,
                notificationMessageTitle,
                JOptionPane.INFORMATION_MESSAGE);
    }

    public Component asWidget() {
        return this;
    }

    private void setWindowPosition( String position ) {
        if ( position == null ){
            windowPosition = null;
        } else if ( position.equals(SoccerPlayer.GOALKEEPER_POSITION) ) {
            windowPosition = GOALKEEPER;
        } else if ( position.equals(SoccerPlayer.DEFENDER_POSITION) ) {
            windowPosition = DEFENDER;
        } else if ( position.equals(SoccerPlayer.MIDFIELDER_POSITION) ) {
            windowPosition = MIDFIELDER;
        } else if ( position.equals(SoccerPlayer.FORWARD_POSITION) ) {
            windowPosition = FORWARD;
        } else {
            windowPosition = null;
        }
    }
}
